#include "game_building.h"

Game_Building::Game_Building(Sence_Config* config){
	this->timer_tick = 0;
	this->p_config = config;
	this->computeDxDyDw();
	Game_Part::LoadPicture(this->left_pictures,this->p_config->Building_Left_Num,this->p_config->Building_Left_Folder);
	Game_Part::LoadPicture(this->right_pictures,this->p_config->Building_Right_Num,this->p_config->Building_Right_Folder);
	this->initialBuilding();
}

Game_Building::~Game_Building(){
	Game_Part::Destroy(this->left_pictures);
	Game_Part::Destroy(this->right_pictures);
	Game_Part::Destroy(this->buildings);
}

void Game_Building::computeDxDyDw(){
	double totald = Game_Part::SumPow2(this->p_config->Building_Tick_Per_Pic);
	double pixels = 0;

	UI::UI_Point& left_p1 = this->p_config->Left_Building_Min_Point;
	UI::UI_Point& left_p2 = this->p_config->Left_Building_Max_Point;
	pixels = left_p2.XPos() - left_p1.XPos();
	this->left_dx = pixels / totald;
	pixels = left_p2.YPos() - left_p1.YPos();
	this->left_dy = pixels / totald;
	pixels = this->p_config->Left_Building_Max_Width - this->p_config->Left_Building_Min_Width;
	this->left_dw = pixels / totald;

	UI::UI_Point& right_p1 = this->p_config->Right_Building_Min_Point;
	UI::UI_Point& right_p2 = this->p_config->Right_Building_Max_Point;
	pixels = right_p2.XPos() - right_p1.XPos();
	this->right_dx = pixels / totald;
	pixels = right_p2.YPos() - right_p1.YPos();
	this->right_dy = pixels / totald;
	pixels = this->p_config->Right_Building_Max_Width - this->p_config->Right_Building_Min_Width;
	this->right_dw = pixels / totald;
}

void Game_Building::addLeftBuilding(){
	int x,y;
	int a = MyRand::Instance()->Next() % this->p_config->Building_Left_Num;
	UI::UI_Point& p1 = this->p_config->Left_Building_Min_Point;
	x = p1.XPos();
	y = p1.YPos();
	int width = this->p_config->Left_Building_Min_Width;
	SDL_Surface* p = Game_Image::ScaleSurface(this->left_pictures[a],width);
	Pic* pic = new Pic(x,y,this->left_dx,this->left_dy,this->left_dw,p,this->left_pictures[a]);
	this->buildings.push_front(pic);
}
void Game_Building::addRightBuilding(){
	int x,y;
	int a = MyRand::Instance()->Next() % this->p_config->Building_Right_Num;
	UI::UI_Point& p1 = this->p_config->Right_Building_Min_Point;
	x = p1.XPos();
	y = p1.YPos();
	int width = this->p_config->Right_Block_Min_Width;
	SDL_Surface* p = Game_Image::ScaleSurface(this->right_pictures[a],width);
	Pic* pic = new Pic(x,y,this->right_dx,this->right_dy,this->right_dw,p,this->right_pictures[a]);
	this->buildings.push_front(pic);
}
void Game_Building::initialBuilding(){
	for(int i=0;i<6;++i)
		this->addLeftBuilding();
	for(int i=0;i<6;++i)
		this->addRightBuilding();
	int step = this->p_config->Building_Tick_Per_Pic * 2 / this->buildings.size();
	int count = 1;
	for(list<Pic*>::iterator it = this->buildings.begin(); it != this->buildings.end(); ++ it){
		if(count > this->buildings.size() / 2)
			count = 1;
		for(int i = 0; i < count * step; ++i){
			(*it)->Move();
		}
		++ count;
	}
}

void Game_Building::moveNext(){
	list<Pic*>::iterator it = this->buildings.begin();
	while(it != this->buildings.end()){
		Pic* p = (*it);
		if(!p->Move()){
			it = this->buildings.erase(it);
			SDL_FreeSurface(p->GetSurface());
			delete p;
			if(MyRand::Instance()->Next() % 2){
				this->addLeftBuilding();
			}
			else{
				this->addRightBuilding();
			}
		}
		else{
			++it;
		}
	}
}

bool Game_Building::TimerTick(){
	bool flag = false;
	++ this->timer_tick;
	if(!(this->timer_tick % this->p_config->Building_Tick_Num)){
		this->moveNext();
		flag = true;
	}
	return flag;
}
void Game_Building::UpdateToScreen(){
	for(list<Pic*>::iterator it = this->buildings.begin(); it != this->buildings.end(); ++ it){
		Game_Part::Add(*it);
	}
}
